/**
 * 分页插件
 * 
 * @author shadow
 * @version v1.0
 */
var sub_page = 10;
var sep_number = 5;

function Pagination() {
};

Pagination.prototype = {
	// 初始化
	initialize : function(offset, limit, total, container) {
		pagination.getHtml(offset, limit, total, container);
	},
	// 回调函数
	callback : function(offset, limit) {
		// extend to do
	},
	// 按钮点击事件
	click : function(offset, limit, total, container) {
		pagination.getHtml(offset, limit, total, container);
		pagination.callback(offset, limit);
	},
	// 获取导航字符串
	getHtml : function(offset, limit, total, container) {
		// 判断条数边界值
		if (limit <= 0)
			limit = 10;
		if (total <= 0)
			total = 0;
		// 计算总共页数
		var page = (total % limit == 0) ? total / limit : Math.ceil(total
				/ limit);
		if (page <= 0)
			return -1;
		// 判断索引边界值
		if (offset <= 0)
			offset = 1;
		if (offset > page)
			offset = page;

		var html = "<div class='spagination'>";

		if (page < 0)
			return "<span>没有找到相应记录</span>";
		if (offset > 1)
			html += "<a href='javascript:pagination.click(" + (offset - 1)
					+ "," + limit + "," + total + ",\"" + container
					+ "\");'>上页</a>";

		if (page <= sub_page)
			html += pagination.getPart(1, page, offset);
		if (page > sub_page) {
			if (offset <= sep_number) {
				html += pagination.getPart(1, sep_number + 2, offset, limit,
						total, container);
				html += pagination.getEllipsis();
				html += pagination.getTagContent(page, limit, total, container);
			} else if (offset > (page - sep_number)) {
				html += pagination.getTagContent(1, limit, total, container);
				html += pagination.getEllipsis();
				html += pagination.getPart(page - (sep_number + 1), page,
						offset, limit, total, container);
			} else {
				html += pagination.getTagContent(1, limit, total, container);
				html += pagination.getEllipsis();
				var temp = Math.floor(sep_number / 2);
				html += pagination.getPart(offset - temp - 1,
						offset + temp + 1, offset, limit, total, container);
				html += pagination.getEllipsis();
				html += pagination.getTagContent(page, limit, total, container);
			}
		}

		// 判断是否显示下页
		if (offset < page)
			html += "<a href='javascript:pagination.click(" + (offset + 1)
					+ "," + limit + "," + total + ",\"" + container
					+ "\");'>下页</a>";
		html += "<div>";
		pagination.append(container, html);
	},
	// 添加导航到容器中
	append : function(container, html) {
		document.getElementById(container).innerHTML = html;
	},
	// 生成按钮链接
	getTagContent : function(offset, limit, total, container) {
		return "<a href='javascript:pagination.click(" + offset + "," + limit
				+ "," + total + ",\"" + container + "\");'>" + offset + "</a>";
	},
	// 生成选中样式按钮链接
	getCurrentTagContent : function(offset, limit, total, container) {
		return "<a class='current' href='javascript:pagination.click(" + offset
				+ "," + limit + "," + total + ",\"" + container + "\");'>"
				+ offset + "</a>";
	},
	// 生成省略部分
	getEllipsis : function() {
		return "<a href='javascript:void(0);'>...</a>";
	},
	// 生成连续按钮部分
	getPart : function(begin, end, offset, limit, total, container) {
		var html = "";
		for (var i = begin; i <= end; i++)
			if (offset == i)
				html += pagination.getCurrentTagContent(i, limit, total,
						container);
			else
				html += pagination.getTagContent(i, limit, total, container);
		return html;
	}
}
// 初始化对象
var pagination = null;
var pagination2 = null;
window.onload = function() {
	pagination = new Pagination();
	pagination.initialize(9, 10, 151, 'page');
	pagination.callback = function(offset, limit) {
		alert("选择按钮: " + offset);
	};

	pagination2 = new Pagination();
	pagination2.initialize(12, 10, 151, 'page2');
	pagination2.callback = function(offset, limit) {
		alert("选择按钮: " + offset);
	};
};